// pages/categories/categories.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalPages: '',
    pageNum: 1,
    goods_list: [],
    indexNav: [],
    curType: '',
    curIndex: 0,
    toView: "new",
    type: '',
    detail: [],
    totalPages: '1',
    windowHeight: '',
    topNum: 0,
    Authorization:''
  },
  tolower() {
    var that = this;
    if (this.data.pageNum >= this.data.totalPages) {
      //表示没有下一页数据
      //console.log("没有下一页数据")
      wx.showToast({
        title: '已经到底了'
      })
    } else {
      wx.showLoading({
        title: '请稍等',
      });
      wx.request({
        url: "http://localhost:8080/wechat/selectgoodsbytype",
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          'Authorization':that.data.Authorization
        },
        data: {
          'pageNum': that.data.pageNum + 1,
          'keyword': that.data.keyword,
        },
        success: function (res) {
          if (res.statusCode == 200){
            wx.hideLoading();
            that.setData({
              goods_list: that.data.goods_list.concat(res.data['goods']),
              pageNum: that.data.pageNum + 1,
            })
            console.info("current pageNum" + that.data.pageNum)
            console.info(that.data.goods_list)
          }else {
            wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
            wx.reLaunch({
              url: '/pages/openid/openid',
            })
          }
        },
      })
    }
  },
  scrolltoupper:function(e){
    console.log(e)
    let t =  e.detail.scrollTop;
    if (t > 100 && !this.data.floorstatus) {
    	// 避免重复setData
    	this.setData({
	       floorstatus: true
	    });
    } 
   	if(t <= 100 && this.data.floorstatus){
  	  this.setData({
        floorstatus: false
      });
   	}
  },
  goTop: function (e) {  // 一键回到顶部
    this.setData({
      topNum: this.data.topNum = 0
    });
  },
  //下拉加载
  /**
   * 生命周期函数--监听页面加载
   */
  switchCategory(e) {
    var type = e.currentTarget.dataset.type;
    wx.setStorageSync('categoriesType', type)
    this.onLoad()
  },
  onLoad: function (options) {
    var that = this;
    var Authorization=wx.getStorageSync('Authorization');
    if(Authorization==''){
      wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.reLaunch({
            url: '/pages/openid/openid',
          })
    }
    that.setData({
      Authorization: Authorization,
    })
    that.setData({
      topNum: this.data.topNum = 0,
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight,
        })
      }
    })
    var type = wx.getStorageSync('categoriesType');
    console.log(type);
    wx.request({
      url: "http://localhost:8080/wechat/selectAllType",
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded",
        'Authorization':that.data.Authorization
      },
      success: function (res) {
        if (res.statusCode == 200){
          that.setData({
            indexNav: res.data,
          })
          if (type == '') {
            that.setData({
              keyword: that.data.indexNav[0].type,
              curType: that.data.indexNav[0].type,
            })
            console.info("type is null:" + that.data.keyword)
          } else {
            that.setData({
              keyword: type,
              curType: type,
            })
            console.info("type is not null:" + that.data.keyword)
          }
          wx.request({
            url: "http://localhost:8080/wechat/selectgoodsbytype",
            method: "POST",
            header: {
              "content-type": "application/x-www-form-urlencoded",
              'Authorization':that.data.Authorization
            },
            data: {
              'pageNum': that.data.pageNum,
              'keyword': that.data.keyword,
            },
            success: function (res) {
              if (res.statusCode == 200){
                that.setData({
                  goods_list: res.data['goods'],
                  totalPages: res.data['totalPages'],
                })
                console.info(that.data.goods_list)
              }else {
                wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
                wx.reLaunch({
                  url: '/pages/openid/openid',
                })
              }
            },
          })
        } else {
          wx.setStorageSync('jwtMessage', "认证信息已过期，请重新登录");
          wx.reLaunch({
            url: '/pages/openid/openid',
          })
        }
       
      },
    })
  },
  showDetail(e) {
    const id = e.currentTarget.dataset.pid;
    console.log(id);
    wx.navigateTo({
      url: `/pages/goods/show?id=${id}`
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})